<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class tire_tread_wear_excel extends Page
{
       public $hfPrevTread = null;
       public $hfPrevOdometer = null;
       public $JTSiteTheme1 = null;
       public $hfTireId = null;
       public $JTGridReports = null;

       function tire_tread_wear_excelShow($sender, $params)
       {
       $this->hfTireId->setValue($_GET['tire_id']);

       $query_comp = "SELECT * FROM company_tbl WHERE company_id='".$_SESSION['company_id']."'";
       $result_comp = mysql_query($query_comp) or die ("SELECT COMP ".mysql_error());
       $row_comp = mysql_fetch_array($result_comp);

       $tire_id = $this->hfTireId->getValue();
       $query_tread_wear = "SELECT * FROM tire_tread_wear_tbl WHERE tire_id='".$tire_id."' ORDER BY id";
       $result_tread_year = mysql_query($query_tread_wear) or die("SELECT TREAD WEAR ".mysql_error());

       $query_tire_inv = "SELECT * FROM tire_inventory_tbl WHERE tire_id='".$tire_id."'";
       $result_tire_inv = mysql_query($query_tire_inv) or die ("SELECT TIRE_INV ".mysql_error());
       $row_tire_inv = mysql_fetch_array($result_tire_inv);

       $query_tire = "SELECT manufacturer, tire_SN, cost, tread_depth FROM tire_inventory_tbl WHERE tire_id='".$tire_id."'";
       $result_tire = mysql_query($query_tire) or die("SELECT TIRE INVENTORY ".mysql_error());
       $row_tire = mysql_fetch_array($result_tire);

       $manufacturer = $row_tire['manufacturer'];
       $tread = $row_tire['tread_depth'];
       $serial = $row_tire['tire_SN'];
       $cost = $row_tire['cost'];

       $cell = array();
       $ctr = 0;

       $cell[] = array(
       '1'=>$row_comp['company_name'],
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'TIRE PERFORMANCE RECORD',
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'',
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'Serial No:'.''.$row_tire_inv['tire_SN'],
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'Branding No:'.''.$row_tire_inv['branding'],
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'Recap No:'.''.$row_tire_inv['recap'],
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'Manufacturer:'.''.$manufacturer,
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );


       $cell[] = array(
       '1'=>'Cost:'.'P'.$cost,
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'',
       '2'=>'',
       '3'=>'',
       '4'=>'',
       '5'=>'',
       '6'=>'',
       '7'=>''
       );

       $cell[] = array(
       '1'=>'DATE',
       '2'=>'ODOMETER',
       '3'=>'TREAD (MM)',
       '4'=>'KM-RUN',
       '5'=>'KM/MM',
       '6'=>'PROJ LIFE',
       '7'=>'CPK'
       );

       while($row_tread = mysql_fetch_array($result_tread_year))
       {
        if($ctr==0)
        {
         $cell[] = array(
         '1'=>'',
         '2'=>$row_tread['odometer'],
         '3'=>$row_tread['tread'],
         '4'=>'',
         '5'=>'',
         '6'=>'',
         '7'=>''
         );
        }
        if($ctr!=0)
        {
         $prev_odometer = $this->hfPrevOdometer->getValue();
         $km_run = $row_tread['odometer'] - $prev_odometer;
         $km_mm = $km_run/($this->hfPrevTread->getValue() - $row_tread['tread']);
         $km_mm = round($km_mm,3);
         $proj_life = $km_mm * $this->hfPrevTread->getValue();
         $proj_life = round($proj_life,3);
         $cpk = $cost/$proj_life;
         $cpk = round($cpk,3);
         $cell[] = array(
         '1'=>$row_tread['insert_date'],
         '2'=>$row_tread['odometer'],
         '3'=>$row_tread['tread'],
         '4'=>$km_run,
         '5'=>$km_mm,
         '6'=>$proj_life,
         '7'=>$cpk
         );
        }
        $this->hfPrevOdometer->setValue($row_tread['odometer']);
        $this->hfPrevTread->setValue($row_tread['tread']);
        $ctr++;
       }
       $this->JTGridReports->setCellData($cell);
       $this->JTGridReports->exportGridToXLSDownload('Tire_Tread_Wear_Report.xls');
       }

}

global $application;

global $tire_tread_wear_excel;

//Creates the form
$tire_tread_wear_excel=new tire_tread_wear_excel($application);

//Read from resource file
$tire_tread_wear_excel->loadResource(__FILE__);

//Shows the form
$tire_tread_wear_excel->show();

?>